package com.example.hw_cacaowithoutinputpicture;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class MessageAdapter extends BaseAdapter{ //implements SendView.OnMessageClickListener{

	public static final int ITEM_VIEW_TYPE_DATE = 0;
	public static final int ITEM_VIEW_TYPE_SEND = 1;
	public static final int ITEM_VIEW_TYPE_RECEIVE = 2;
	public static final int ITEM_VIEW_COUNT = 3;
	
	List<MessageData> mList;
	Context mContext;
	
//	public interface OnMessageViewClickListener{
//		public void onMessageViewClick(Adapter a, View v, MessageData data);
//	}
//	
//	OnMessageViewClickListener mListener;
//	
//	public void setOnMessageViewClickListener(OnMessageViewClickListener listener){
//		mListener = listener;
//	}
	
	public MessageAdapter(Context context, List<MessageData> list) {
		// TODO Auto-generated constructor stub
		mContext = context;
		mList = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		MessageData data = mList.get(position);
		
		if(data.image != null){
			PictureView view;
			if(convertView == null){
				view = new PictureView(mContext);
			}else{
				view = (PictureView) convertView;
			}
			view.setData(data);
			return view;
		}else if(data.message == null){
			DateView view;
			if(convertView == null){
				view = new DateView(mContext);
			}else{
				view = (DateView) convertView;
			}
			view.setData(data);
			return view;
		}else if(data.isChecked == true){
			SendView view;
			if(convertView == null){
				view = new SendView(mContext);
			}else{
				view = (SendView) convertView;
			}
			
			view.setData(data);
			return view;
		}else{
			ReceiveView view;
			if(convertView == null){
				view = new ReceiveView(mContext);
			}else{
				view = (ReceiveView) convertView;
			}
			
			view.setData(data);
			return view;
		}
		
		
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		MessageData data = mList.get(position);
		if(data.message == null)
			return ITEM_VIEW_TYPE_DATE;
		if(data.isChecked == true)
			return ITEM_VIEW_TYPE_SEND;
		else
			return ITEM_VIEW_TYPE_RECEIVE;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return ITEM_VIEW_COUNT;
	}

	public void add(MessageData messageData) {
		// TODO Auto-generated method stub
		mList.add(messageData);
		notifyDataSetChanged();
	}

//	@Override
//	public void onMessageClick(View v, MessageData data) {
//		// TODO Auto-generated method stub
//		if(mListener != null)
//			mListener.onMessageViewClick(this, v, data);
//	}

}
